package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import model.GroceryItem;
import model.GroceryListDao;

/**
 * The ItemListPanel for displaying the items in the shopping list
 * @author Lawrence Grass
 */
@SuppressWarnings("serial")
public class ItemListPanel extends JPanel{
	
	/**
	 * this panels parent
	 */
	private GroceryListFrame frame;
	
	private GroceryListDao dao;
	
	private JPanel listView;
	
	private int largestSizeItem = 0;
	
	/**
	 * Constructor for making a panel that list the shopping list items
	 * @param parent the parent of this panel
	 */
	public ItemListPanel(GroceryListFrame frame, GroceryListDao dao){
		//sets up the view for displaying the list you're looking at
		super();
		this.dao = dao;
		this.frame = frame;
		this.setLayout(new BorderLayout());
		
		//set up the list of items in your shopping list
		listSetup();
		JScrollPane sp = new JScrollPane(listView);
		sp.setPreferredSize(new Dimension(380, 550));
		this.add(sp);
		
		//sets up the controls to manipulate the list
		this.add(controlsSetup(), BorderLayout.SOUTH);
	}
	
	/**
	 * helper method for the list panel
	 * @return the list panel
	 */
	private JPanel listSetup(){
		listView = new JPanel();
		refreshItemsList();
		return listView;
	}
	
	/**
	 * helper method for the controls panel
	 * @return the controls panel
	 */
	private JPanel controlsSetup(){
		JPanel controlsPanel = new JPanel();
		controlsPanel.setPreferredSize(new Dimension(200, 50));
		controlsPanel.setBackground(Color.gray);
		JButton addItem = new JButton("Add Item");
		addItem.addActionListener(frame);
		addItem.setPreferredSize(new Dimension(120, 20));
		controlsPanel.add(addItem);
		JButton viewHistory = new JButton("View History");
		viewHistory.addActionListener(frame);
		viewHistory.setPreferredSize(new Dimension(120, 20));
		controlsPanel.add(viewHistory);
		JButton viewNutrition = new JButton("Nutrition Info");
		viewNutrition.addActionListener(frame);
		viewNutrition.setPreferredSize(new Dimension(120, 20));
		controlsPanel.add(viewNutrition);
		
		JButton printList = new JButton("Print");
		printList.addActionListener(frame);
		printList.setPreferredSize(new Dimension(120, 20));
		controlsPanel.add(printList);
		return controlsPanel;
	}
	
	public void refreshItemsList(){
		largestSizeItem = 0;
		listView.removeAll();
		try {
			List<GroceryItem> list = dao.getGroceryList();
			for(final GroceryItem item: list){
				JPanel row = new JPanel();
				row.setPreferredSize(new Dimension(400, 40));
				if(item.getItem().length() > largestSizeItem)
				{
					 largestSizeItem = item.getItem().length();
				}
				if(item.getItem().length() > 20)
				{
					row.setPreferredSize(new Dimension((400 + item.getItem().length() * 4), 40));
				}
				JLabel itemName = new JLabel("Item: " + item.getItem() + ", ");
				row.add(itemName);
				JLabel itemPrice = new JLabel("Price: " + item.getPrice().toString() + ", ");
				row.add(itemPrice);
				JLabel itemSale = new JLabel();
				if(item.isOnSale()){
					itemSale.setText("Sale: Yes, ");
				} else {
					itemSale.setText("Sale: No, ");
				}
				row.add(itemSale);
				JButton remove = new JButton("Remove");
				remove.addActionListener(new ActionListener() 
				{
			        public void actionPerformed(ActionEvent event) 
			        {

			    		dao.deleteItem(item.getItem());
			    		frame.refreshCards();
			    		revalidate();
			    		validate();
			        }
			    });	
				row.add(remove);
				listView.add(row);
				listView.setPreferredSize(new Dimension(380, list.size() * 50));
				if(largestSizeItem > 20)
				{
					listView.setPreferredSize(new Dimension((400 + largestSizeItem * 4), list.size() * 50));
				}
				
			}
	
		} catch (Exception e) {
			//Do nothing?
			e.printStackTrace();
		}
	}

}
